package com.gang.music.system.controller;

import com.gang.music.common.AjaxResult;
import com.gang.music.core.annotation.Before;
import com.gang.music.core.intercept.RegisterValidator;
import com.gang.music.system.entity.User;
import com.gang.music.system.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户注册的Controller.
 * 
 */
@Controller
@RequestMapping(value = "/register")
public class RegisterController extends BaseController {

	@Autowired
	private UserService userService;

	@RequestMapping(method = RequestMethod.GET)
	public String registerForm() {
		return "web/register";
	}

	@Before(RegisterValidator.class)
	@ResponseBody
	@RequestMapping(method = RequestMethod.POST)
	public AjaxResult register(User user) {
		try {
			userService.registerUser(user);
			return success();
		} catch (Exception e) {
			e.printStackTrace();
			return error("系统出错,请稍后再试!");
		}
	}

	/**
	 * Ajax请求校验loginName是否唯一。
	 */
	@RequestMapping("/checkLoginName")
	@ResponseBody
	public AjaxResult checkLoginName(@RequestParam("loginName") String loginName) {		
		return userService.ckeckLoginName(loginName)? success():error();	
	}
}
